package com.raychi.demo.servlet.company;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.raychi.demo.entity.Company;
import com.raychi.demo.util.CompanyUtil;
import com.raychi.demo.util.JdbcUtil;

public class CompanySevlet extends HttpServlet{


	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			List<Company> list = getCompanies();
			request.setAttribute("list", list);
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
			
	}
	
	private List<Company> getCompanies() throws ClassNotFoundException, SQLException{
		List<Company> companies = new ArrayList<Company>();
		Connection conn = JdbcUtil.getConnection();
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery("select * from company");
		Company company = null;
		while(rs.next()){
			company = CompanyUtil.createCompany(rs);
			companies.add(company);
		}
		return companies;
	}
	
	
	
}
